﻿#region 驱动
using System;
using System.Drawing;
using System.Windows.Forms;
using Lz.串口;
using Sunisoft.IrisSkin;
using System.IO;
#endregion
namespace 调试者
{
    public partial class TS调试者 : Form
    {
        #region 变量
        COM com = new COM();
        #endregion

        #region 重要的函数
        private void comdk打开()
        {
            try
            {
                com.PortName = commz名字.Text;
                com.BaudRate = Convert.ToInt32(combt波特率.Text);
                com.Parity = COM.StringToParity(comjy校验位.Text);
                com.DataBits = Convert.ToInt16(comsj数据位.Text);
                com.StopBits = COM.StingToStopBits(comtz停止位.Text);
                com.Open();
                if (com.IsOpen)
                {
                    but打开.Text = "关闭";
                    labzt状态.BackColor = Color.Red;
                    labckx串口信息();
                    throw new Exception("串口打开成功！");
                }
                else
                {
                    throw new Exception("串口打开失败！请检查是否存在此串口。");
                }
            }
            catch (Exception ex)
            {
                fill状态条2(ex.Message);
            }
        }
        private void comgb关闭()
        {
            try
            {
                if (com.IsOpen)
                {
                    com.Close();
                    if (!com.IsOpen)
                    {
                        but打开.Text = "打开";
                        labzt状态.BackColor = Color.Blue;
                        throw new Exception("串口关闭成功！");
                    }
                    else
                    {
                        throw new Exception("串口关闭失败！");
                    }
                }
                else
                {
                    //串口已经被关闭了。我的笔记本上用的是usb--com转换器，经常需要重新插一下，松动时就非法关闭了，会出现这种情况。
                    //throw new Exception("不会出现这种情况。");

                    but打开.Text = "打开";
                    labzt状态.BackColor = Color.Blue;
                    throw new Exception("串口信息出错，请检查是否存在这个串口。");
                }
            }
            catch (Exception ex)
            {
                fill状态条2(ex.Message);
            }
        }
        private void comcx重新打开()
        {
            comgb关闭();
            comdk打开();
        }
        private void fs发送(string _fs)
        {
            if (_fs.Length > 0)
            {
                try
                {
                    byte[] bt =TiaoShiQi.StringToBytes(_fs, checkfs16进制发送.Checked);
                    com.Write(bt, 0, bt.Length);
                    TiaoShiQi.sjcl数据处理(this, richfsq发送区, bt, checkfs16xs发送区16进制显示.Checked, checkfszfxs发送区字符显示.Checked, checkfsxhxs发送区新行显示.Checked);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message.ToString());
                }
            }
            else
            {
                throw new Exception("发送失败，你没有填写任何数据。");
            }
        }        
        private void com接收(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                byte[] bt = new byte[com.BytesToRead];
                com.Read(bt, 0, com.BytesToRead);
                if (bt.Length > 0)
                {
                    TiaoShiQi.sjcl数据处理(this, richjsq接收区, bt, checkjs16xs接收区16进制显示.Checked, checkjszfxs接收区字符显示.Checked, checkjsxhxs接收区新行显示.Checked);
                }
            }
            catch (Exception ex)
            {
                fill状态条2(ex.Message.ToString());
            }
        }
        #endregion

        #region 系统生成
        private void TS调试者_Load(object sender, EventArgs e)
        {
            com.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(com接收);//串口接到数据会激发这个事件
            commz名字.Items.AddRange(COM.AllCom());
            commz名字.SelectedIndex = 0;
            foreach (int i in COM.AllBaudRate())
            {
                combt波特率.Items.Add(i);
            }
            combt波特率.SelectedIndex = 6;
            foreach (string s in COM.AllParity())
            {
                comjy校验位.Items.Add(s);
            }
            comjy校验位.SelectedIndex = 0;
            foreach (int i in COM.AllDataBits())
            {
                comsj数据位.Items.Add(i);
            }
            comsj数据位.SelectedIndex = 2;
            foreach (string s in COM.AllStopBits())
            {
                comtz停止位.Items.Add(s);
            }
            comtz停止位.SelectedIndex = 0;
            duqu();//读取皮肤文件
        }
        private void but打开_Click(object sender, EventArgs e)//打开按钮
        {
            try
            {
                if (but打开.Text == "打开")
                {
                    comdk打开();
                }
                else
                {
                    comgb关闭();
                }
            }
            catch (Exception ex)
            {
                fill状态条2(ex.Message.ToString());
            }
        }
        private void butfs发送_Click(object sender, EventArgs e)//发送按钮
        {
            try
            {
                if (com.IsOpen)
                {
                    try
                    {
                        fs发送(richfs发送.Text.ToString());
                        throw new Exception("发送成功。");
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message.ToString());
                    }
                }
                else
                {
                    throw new Exception("串口没有打开。");
                }
            }
            catch (Exception ex)
            {
                toolStripStatusLabel2.Text = ex.Message.ToString();
            }
        }
        private void butjs接收区清空_Click(object sender, EventArgs e)//发送区清空
        {
            richjsq接收区.Text = "";
        }
        private void butfs发送区清空_Click(object sender, EventArgs e)//发送区清空
        {
            richfsq发送区.Text = "";
        }
        private void butfsq发送清空_Click(object sender, EventArgs e)//发送清空
        {
            richfs发送.Text = "";
        }
        private void timer1_Tick(object sender, EventArgs e)//自动发送时间控制
        {
            try
            {
                if (com.IsOpen)
                {
                    fs发送(richfs发送.Text.ToString());
                    throw new Exception("发送成功。");
                }
                else
                {
                    throw new Exception("串口没有打开。");
                }
            }
            catch (Exception ex)
            {
                toolStripStatusLabel2.Text = ex.Message.ToString();
            }
        }
        private void checkzd自动发送_CheckedChanged(object sender, EventArgs e)//自动发送选项
        {
            if (checkzd自动发送.Checked)
            {
                timer1.Interval = Convert.ToInt16(textZq周期.Text.ToString());
                timer1.Enabled = true;
            }
            else
            {
                timer1.Enabled = false;
            }
        }
        private void commz名字_SelectedIndexChanged(object sender, EventArgs e)//重新设置串口
        {
            comcx重新打开();
        }
        #endregion

        #region 换皮肤
        private SkinEngine skinEngine1 = new SkinEngine();
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = comboBox1.Text.ToString();
        }
        public void duqu()
        {
            string[] a = Directory.GetFiles("pi");
            foreach (string file in a)
            {
                comboBox1.Items.Add(file);
            }
        }
        #endregion

        #region 无用
        public TS调试者()
        {
            InitializeComponent();
        }
        private void TS调试者_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (com.IsOpen)
                {
                    com.Close();
                }
            }
            catch (Exception ex)
            {
                fill状态条2(ex.Message);
            }
        }
        private void fill状态条2(string s)
        {
            toolStripStatusLabel2.Text = s.ToString();
        }
        private void labckx串口信息()
        {
            textBox1.Text = com.PortName.ToString() + "," + com.BaudRate.ToString() + "," + COM.ParityToString(com.Parity) + "," + com.DataBits.ToString() + "," +COM.StopBitsToSting(com.StopBits);
        }
        Icon i1 = Resource1.I1;
        Icon i2 = Resource1.I2;
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (Icon==i1)
            {
                Icon =i2;
            }
            else
            {
                Icon = i1;
            }
        }
        private void 复制_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.SourceControl.Select();//先获取焦点，防止点两下才运行
            RichTextBox rtb = (RichTextBox)this.contextMenuStrip1.SourceControl;
            rtb.Copy();
        }
        private void 粘贴_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.SourceControl.Select();
            RichTextBox rtb = (RichTextBox)this.contextMenuStrip1.SourceControl;
            rtb.Paste();
        }
        private void 剪切_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.SourceControl.Select();
            RichTextBox rtb = (RichTextBox)this.contextMenuStrip1.SourceControl;
            rtb.Cut();
        }
        private void 删除_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.SourceControl.Select();
            RichTextBox rtb = (RichTextBox)this.contextMenuStrip1.SourceControl;
            rtb.SelectedText = "";
        }
        private void 全选_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.SourceControl.Select();
            RichTextBox rtb = (RichTextBox)this.contextMenuStrip1.SourceControl;
            rtb.SelectAll();
        }
        private void 撤销_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.SourceControl.Select();
            RichTextBox rtb = (RichTextBox)this.contextMenuStrip1.SourceControl;
            rtb.Undo();
        }
        #endregion

        int num = 0;
        private void toolStripStatusLabel4_Click(object sender, EventArgs e)
        {
            num += 1;
            if (num > 10)
            {
                关于 guan = new 关于();
                guan.ShowDialog();
                num = 0;
            }
        }
    }
}